package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"os"
)

/**
* @description: 获取账户信息
* @param {*}
* @return {*}
 */
func getAccountInfo() {
	url := "https://devnet.helius-rpc.com/?api-key=a4b0ed9e-8646-4918-8b5c-4f5dade06e4b"

	requestBody := map[string]interface{}{
		"jsonrpc": "2.0",
		"id":      "1",
		"method":  "getMultipleAccounts",
		"params":  []interface{}{[]string{"6Qwwa1WU1ZkxXXoTep2mCjdJ3d2SuNbHeEZjR7ARnwsB"}},
	}

	jsonData, err := json.Marshal(requestBody)

	fmt.Print("requestData:%s\n", requestBody)

	resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))

	if err != nil {
		log.Fatal(err)
	}

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Response body: %s\n", body)

}

/**
 * 查询某个区块的信息
 */
func getBlock(block int) {

	url := "https://devnet.helius-rpc.com/?api-key=a4b0ed9e-8646-4918-8b5c-4f5dade06e4b"

	requestBody := map[string]interface{}{
		"jsonrpc": "2.0",
		"id":      "1",
		"method":  "getBlock",
		"params": []interface{}{
			block,
			map[string]interface{}{
				"encoding":                       "jsonParsed",
				"maxSupportedTransactionVersion": 0, // ✅ 关键：支持 v0 交易
			},
		},
	}

	jsonData, err := json.Marshal(requestBody)

	fmt.Print("requestData:%s\n", requestBody)

	resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))

	if err != nil {
		log.Fatal(err)
	}

	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	outputFile := fmt.Sprintf("block_%d.json", block)
	err = os.WriteFile(outputFile, body, 0644)
	if err != nil {
		log.Fatalf("Error writing file: %v", err)
	}

	fmt.Printf("Response body: %s\n", body)
}

// getBlocks 获取指定 slot 范围内的区块列表
func getBlocks(startSlot, endSlot int) {
	// 替换为你的 Helius API Key
	apiKey := "a4b0ed9e-8646-4918-8b5c-4f5dade06e4b"
	url := "https://devnet.helius-rpc.com/?api-key=" + apiKey

	// 构建请求体
	requestBody := map[string]interface{}{
		"jsonrpc": "2.0",
		"id":      "1",
		"method":  "getBlocks",
		"params": []interface{}{
			startSlot,
			endSlot,
			map[string]interface{}{
				"commitment": "finalized", // 可选：finalized / confirmed / processed
			},
		},
	}

	// 序列化为 JSON
	jsonData, err := json.Marshal(requestBody)
	if err != nil {
		log.Fatalf("JSON marshal error: %v", err)
	}

	fmt.Printf("Request Data: %s\n", jsonData)

	// 发送 POST 请求
	resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		log.Fatalf("HTTP request failed: %v", err)
	}
	defer resp.Body.Close()

	// 检查状态码
	if resp.StatusCode != http.StatusOK {
		body, _ := io.ReadAll(resp.Body)
		log.Fatalf("HTTP Error %d: %s", resp.StatusCode, string(body))
	}

	// 读取响应体
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatalf("Read response body failed: %v", err)
	}

	// 打印响应
	fmt.Printf("Response Status: %s\n", resp.Status)
	fmt.Printf("Response Body: %s\n", body)

	// 👇 保存到文件
	outputFile := fmt.Sprintf("blocks_from_%d_to_%d.json", startSlot, endSlot)
	err = os.WriteFile(outputFile, body, 0644)
	if err != nil {
		log.Printf("Failed to write file: %v", err)
	} else {
		fmt.Printf("Response saved to %s\n", outputFile)
	}
}

func main() {

	// getAccountInfo()
	// getBlock(418480966)
	// getBlock(418106935)
	getBlocks(418099505, 418099663)
}
